if __name__ == '__main__':
    import pandas as pd

    # 示例日期数据
    date_list = pd.to_datetime(['2023-01-01', '2023-02-15', '2023-03-10', '2023-04-05', '2023-04-01'])

    # 定义分桶边界
    bins = pd.to_datetime(['2023-01-01', '2023-03-01', '2023-04-01', '2023-06-01'])

    # 分桶操作
    labels = ['Q1', 'Q2', 'Q3']  # 每个区间的标签
    binned = pd.cut(date_list, bins=bins, labels=labels)

    print(binned)
    print("*" * 50)
    # 创建一个包含日期和值的数据框
    df = pd.DataFrame({
        'date': pd.to_datetime(['2023-01-01', '2023-01-15', '2023-02-01', '2023-03-10']),
        'value': [10, 20, 30, 40]
    })

    # 按月分组并聚合
    monthly_df = df.groupby(pd.Grouper(key='date', freq='ME')).sum()
    print(monthly_df)
    print("*" * 50)

    # 设置索引为日期
    df.set_index('date', inplace=True)

    # 按月重采样并求和
    resampled_df = df.resample('ME').sum()
    print(resampled_df)
    print("*" * 50)
    # 计算每个日期距离起始点的天数差
    start_date = date_list.min()
    days_diff = (date_list - start_date).days

    # 按每7天分桶
    bucket_size = 7
    binned_by_week = pd.cut(days_diff, bins=range(0, days_diff.max() + bucket_size, bucket_size))

    print(binned_by_week)
